package com.putong35.common.account;


import android.accounts.Account;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;

import com.putong35.common.BR;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * 账号类
 * <p>
 * 数据直接写到sp里，从内存中读取
 * <p>
 * user_id : test
 * real_name : 真实姓名
 * sign : 签名
 * portrait :
 * phone : 18088888888
 * gender : WOMAN
 * company : 爬虫
 * department : IT事业部
 * position : null
 * ext_info :
 * dateline : 1468217954
 *
 * @Author: PV
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2017/04/06 16:35
 * @Version: 1.0.0
 */
public class OldAccount extends BaseObservable implements Serializable {

    private String id;
    private String name;
    private String portraitUrl;
    private String phone;
    private long birthday;

    public OldAccount() {
    }


    /**
     * 克隆一个对象
     * 采用Serializable, 实现深度克隆
     * @return
     */
    public Account clone() {
        Account outer = null;
        try { // 将该对象序列化成流,因为写在流里的是对象的一个拷贝，而原对象仍然存在于JVM里面。所以利用这个特性可以实现对象的深拷贝
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(this);
            // 将流序列化成对象
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bais);
            outer = (Account) ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return outer;
    }

    @Bindable
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
        notifyPropertyChanged(BR.id);

    }

    @Bindable
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        notifyPropertyChanged(BR.name);
    }

    @Bindable
    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
        notifyPropertyChanged(BR.phone);
    }

    @Bindable
    public String getPortraitUrl() {
        return portraitUrl;
    }

    public void setPortraitUrl(String portraitUrl) {
        this.portraitUrl = portraitUrl;
        notifyPropertyChanged(BR.portraitUrl);
    }

    @Bindable
    public long getBirthday() {
        return birthday;
    }

    public void setBirthday(long birthday) {
        this.birthday = birthday;
        notifyPropertyChanged(BR.birthday);
    }


    @Override
    public String toString() {
        return "Account{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", portraitUrl='" + portraitUrl + '\'' +
                ", phone='" + phone + '\'' +
                ", birthday='" + birthday + '\'' +
                '}';
    }
}
